<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/doctor'}">医生信息</el-breadcrumb-item>
      <el-breadcrumb-item>新增医生</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 定义卡片视图 -->
    <el-card class="box-card">

      <!-- 定义页面提示信息 -->
      <el-alert title="新增医生流程" type="info" effect="dark" center show-icon :closable="false"></el-alert>

      <!-- 定义步骤条 字符串需要转化为数值 -->
      <el-steps :active="activeIndex - 0" finish-status="success" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="主治症状"></el-step>
      </el-steps>


      <!-- 定义标签页 before-leave:切换标签之前的钩子，若返回 false 或者返回 Promise 且被 reject，则阻止切换。 -->
      <el-form :model="addhospitalForms" :rules="addhospitalFormsRules" ref="addhospitalFormsRef" label-width="100px"
        label-position="top">
        <el-tabs v-model="activeIndex" :tab-position="'left'" :before-leave="beforeLeave">
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="addhospitalForms.name"></el-input>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
              <el-input v-model="addhospitalForms.age" type="number"></el-input>
            </el-form-item>
          <el-form-item label="性别" prop="sex">
            </el-form-item>
            <el-form-item label="科室" prop="sex">
              <!-- 通过级联获取商品分类信息-->
              <el-cascader v-model="deparments" :options="hospitalList" :props="props"  @change="changeCascader">
              </el-cascader>
            </el-form-item>
            <el-form-item label="主治症状" prop="bygoods">
                <el-input type="textarea" v-model="addhospitalForms.bygood"></el-input>
            </el-form-item>
          </el-tab-pane>
            <!-- 定义添加商品按钮-->
            <el-button type="primary" class="addItemBtnClass" @click="addItemBtn">新增医生</el-button>
        </el-tabs>
      </el-form>

    </el-card>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        radio: 1,
        activeIndex: '0',
        addhospitalForms: {
          name: '',
          age: 0,
          sex: '',
          bygood: '',
          department: ''
          //dynamicArgs: [],
          //staticArgs: []
        },
        addhospitalFormsRules: {
          name: [{
            required: true,
            message: '请输入医生姓名',
            trigger: 'blur'
          }],
          age: [{
            required: true,
            message: '请输入医生年龄',
            trigger: 'blur'
          }],
          sex: [{
            required: true,
            message: '请输入医生性别',
            trigger: 'blur'
          }],
          bygoods: [{
            required: true,
            message: '请输入主治症状',
            trigger: 'blur'
          }],
        },
        deparments: [],
        hospitalList: [],
        props: {
          expandTrigger: 'hover',
          value: "id", //选中数据的value值
          label: "name", //选中数据展现名称
          children: "children", //自选项数据
        },
        //设置商品动态参数
      }
    },
    created() {
      this.findhospitalList()
    },
    methods: {
      //1.获取所有商品分类信息
      async findhospitalList() {
        const {
          data: result } = await this.$http.get("/hospital/findHospitalList/2")
        this.hospitalList = result.data
      },
      //修改商品分类选项
     async changeCascader() {
        const {
          data: result } =  await this.$http.get("/hospital/findHospitalById",{
            params: {
              id: this.deparments[1]
            }
          })
        this.addhospitalForms.department = result.data
      },
      //当标签页数据没有完成选择时 不让切换标签页
     beforeLeave(activeName, oldActiveName) {
        //注意oldActiveName的数据类型为字符串
        if (this.deparments.length !== 3 && oldActiveName === '0') {
          this.$message.error("请先选择商品分类")
          return false
        }
      },
      /* 添加商品按钮 */
     async addItemBtn(){
       //1.完成表单校验
       this.$refs.addhospitalFormsRef.validate( valid => {
         if(!valid) return this.$message.error("请输入商品必填项")
       })

       //2.完成商品参数的封装
       //2.5 实现商品数据提交
       let {data: result} = await this.$http.post("/doctor/saveDoctor",this.addhospitalForms)
         if(result.status !== 200)
         return this.$message.error("添加失败")
       this.$message.success("医生信息添加成功")
       //2.5添加完成之后,将数据重定向到商品展现页面
       this.$router.push("/doctor")
     }
    }
  }
</script>

<style lang="less" scoped>
  .el-steps {
    margin: 20px 0;
  }

  .el-cascader {
    width: 30%;
  }

  /* 控制复选框的右边距5像素 !important 表示优先执行 IE6不兼容*/
  .el-checkbox {
    margin: 0 10px 0 0 !important;
  }

  .addItemBtnClass{

    margin-top: 15px;
  }

</style>
